package Logic;

public class Square {
	
	public enum Rank {
		Bomb(-1),
		Captain(5),
		Colonel(7),
		Flag(-2),
		General(8),
		Lieutenant(4),
		Major(6),
		Marshal(9),
		Miner(2),
		Scout(1),
		Sergeant(3),
		Spy(0),
		Unknown(-3);
		
		public static Rank valueOf(int rankId) {
			for (Rank rank : Rank.values()) {
				if (rank.getId() == rankId) return rank;
			}
			return null;
		}
		private int id;
		
		Rank(int id) {
			this.id = id;
		}

		public int getId() {
			return id;
		}
	}
	
	public enum Type {
		Free(false,true), Null(false,false), Soldier(true,true), Water(false,false);
		
		private boolean occupied;
		private boolean walkable;

		Type(boolean occupied, boolean walkable) {
			this.occupied = occupied;
			this.walkable = walkable;
		}

		public boolean isOccupied() {
			return occupied;
		}

		public boolean isWalkable() {
			return walkable;
		}
	}
	
	public Square(int playerId, Rank rank) {
		this(Type.Soldier, playerId, rank);
	}

	public Square(Type type) {
		this(type,0,null);
	}

	private Square(Type type, Integer playerId, Rank rank) {
		this.playerId = playerId;
		this.rank = rank;
		this.type = type;
	}
	
	private Integer playerId = -12;
	private Rank rank;
	private Type type;

	/* create new ISquare same as the original one */
	public Square duplicateInstance() {
		return new Square(type,playerId,rank);
	}
	
	public int getPlayerId() {
		
		if (playerId == null) return 0;
		return playerId;
		
	}
    public Rank getRank() {
		return rank;
	}
    public void setRank(Rank rank) {
		this.rank = rank;
	}
    
    public Type getType() {
		return type;
	}
	public boolean isOccupied() {
    	return getType().isOccupied();
    }
	
	public boolean isWalkable() {
    	return getType().isWalkable();
    }
	
	@Override
	public String toString() {
		return getType().toString() + ": " + getPlayerId(); 
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Square)) return false;
		Square square = (Square) obj;
		if (this.getType() != square.getType() ||
				this.getRank() != square.getRank() ||
				this.getPlayerId() != square.getPlayerId()) return false;
		return true;
	}

	public void setFree() {
		type = Type.Free;
		rank = null;
		playerId = null;
	}

	public void setPlayerId(Integer playerId) {
		this.playerId = playerId;
	}	
}